﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data

Imports System.Net.Mail


Namespace Arwani
    Partial Class CustomerInvoice
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                If Request.Cookies("Code") Is Nothing Then

                    Response.Redirect("index.aspx")
                Else
                    bindgrid()
                    lblCustomer.Text = Request.Cookies("Name").Value
                End If
            End If
        End Sub
        Protected Sub bindgrid()
            Dim awrcon As New ArwaniController
            Dim str As String = "SELECT IDENTITY (int, 1, 1) AS [ID], a.Articleno, a.Serialno, a.invno as [Invoice No.],a.Manuals, b.DELDATE as [Invoice Date] into #temptry " & _
                "FROM serviceaftersale a INNER JOIN INVOICEMASTER b ON a.invno = b.INVOICENO " & _
                "WHERE (a.custcode = '" & Request.Cookies("Code").Value & "') and " & _
                "(len(ltrim(rtrim(a.q1invno))) <> 0 or len(ltrim(rtrim(a.q2invno))) <> 0 or len(ltrim(rtrim(a.q3invno))) <> 0 or len(ltrim(rtrim(a.q4invno))) <> 0 ) " & _
                "GROUP BY a.Articleno, a.serialno, a.invno,a.Manuals, b.DELDATE order by b.deldate,a.articleno " & _
                "SELECT * FROM #Temptry ORDER BY [ID] DROP TABLE #Temptry "
            Dim dtTable As DataTable = awrcon.ExecuteQuery(str)
            If dtTable.Rows.Count > 0 Then
                gridViewMaster.DataSource = dtTable
                gridViewMaster.DataBind()

            End If
        End Sub


        Protected Sub gridViewMaster_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridViewMaster.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim awrcon1 As New ArwaniController
                Dim customerID As String = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ID"))
                Dim gridViewNested As GridView = DirectCast(e.Row.FindControl("nestedGridView"), GridView)
                Dim lblarticleNo As String = DirectCast(e.Row.FindControl("lblarticleNo"), Label).Text
                Dim lblSerialNo As String = DirectCast(e.Row.FindControl("lblSerialNo"), Label).Text
                Dim strQuery As String = "select * from serviceaftersale where articleno = '" & Trim(lblarticleNo) & "' and serialno = '" & Trim(lblSerialNo) & "' and custcode = '" & Request.Cookies("Code").Value & "' and (len(ltrim(rtrim(q1invno))) <> 0 or len(ltrim(rtrim(q2invno))) <> 0 or len(ltrim(rtrim(q3invno))) <> 0 or len(ltrim(rtrim(q4invno))) <> 0 ) order by [year]"
                Dim dtTable1 As DataTable = awrcon1.ExecuteQuery(strQuery)
                Dim j As Integer
                Dim x As Integer
                Dim i As Integer
                Dim dt As New DataTable
                Dim maintable As New DataTable

                maintable.Columns.Add("invoiceno")
                maintable.Columns.Add("totalamt")
                maintable.Columns.Add("deldate")
                maintable.Columns.Add("jobcard")

                For j = 0 To dtTable1.Rows.Count - 1
                    For x = 1 To 4
                        Dim SQLstr As String = "select invoiceno,totalamt,deldate,jobcard from servicemaster where invoiceno = '" & Trim(IIf(IsDBNull(dtTable1.Rows(j).Item("Q" & x & "Invno")), "", dtTable1.Rows(j).Item("Q" & x & "Invno"))) & "'"
                        dt = awrcon1.ExecuteQuery(SQLstr)
                        If dt.Rows.Count > 0 Then


                            For i = 0 To dt.Rows.Count - 1
                                Dim R As DataRow = maintable.NewRow
                                R("invoiceno") = dt.Rows(i)("invoiceno")
                                R("totalamt") = CDec(dt.Rows(i)("totalamt")).ToString("f2")
                                R("deldate") = CDate(dt.Rows(i)("deldate")).ToString("dd/MM/yyyy")
                                R("jobcard") = dt.Rows(i)("jobcard")
                                maintable.Rows.Add(R)

                            Next
                        End If
                    Next

                Next
                gridViewNested.DataSource = maintable
                gridViewNested.DataBind()
            End If
        End Sub
        Protected Sub nestedGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim awrcon1 As New ArwaniController
                Dim gridViewNested2 As GridView = DirectCast(e.Row.FindControl("nestedGridView2"), GridView)
                Dim invoiceno As String = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "invoiceno"))
                Dim strQuery As String = "select * from servicedetail where invoiceno = '" & Trim(invoiceno) & "'"
                Dim dtTable1 As DataTable = awrcon1.ExecuteQuery(strQuery)
                gridViewNested2.DataSource = dtTable1
                gridViewNested2.DataBind()
            End If
        End Sub



        Protected Function GetCheckedOutValue(checkedOut As String) As String
            If checkedOut = "" Then
                Dim ret As String = "<a href='javascript:void(0);'>No Job Card</a>"

                Return ret

            Else

                Dim ret As String = "<a href='../Invoices/" & checkedOut & "' target='_blank'>Job Card</a>"
                Return ret
            End If
        End Function


        Protected Function GetCheckedOutValueM(checkedOut As String) As String
            If checkedOut = "" Then
                Dim ret As String = "<a href='javascript:void(0);'>No Manuals</a>"

                Return ret

            Else

                Dim ret As String = "<a href='../Manuals/" & checkedOut & "' target='_blank'>Manuals</a>"
                Return ret
            End If
        End Function



        Protected Sub lnkRequestForService_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkRequestForService.Click
            Session("CompanyName") = lblCustomer.Text
            Response.Redirect("RequestforService.aspx")


        End Sub
    End Class
End Namespace